package cn.edu.jxau.framework.dao.core;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import cn.edu.jxau.framework.dao.entity.View;
import cn.edu.jxau.framework.dao.exception.ConfigurationException;



/**
 * 解析配置文件
 * @author Aaron
 *
 */
public class ParseXML {
	

	private Document document;
	private Element  configuration;
	private Element views;
	
	//单例
	private static ParseXML parseXML;
	
	//创建Parse实例
	public static  int CreateParseXMLInstance(String fileUrl){
		if(parseXML==null){
			try {
				parseXML = new ParseXML(fileUrl);
				return 1;
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return -1;
	}
	//返回parseXML实例
	public static ParseXML getInstance(){
		return parseXML;
	}
	
	private ParseXML(String fileUrl) throws DocumentException{
		
		SAXReader reader = new SAXReader(); 

		//获取配置文件路径url
		URL url = ParseXML.class.getResource(fileUrl);
		document = reader.read(url);
		configuration = document.getRootElement();
		views = configuration.element("views");
	}
	
	public HashMap<String,View> parseViews(){
		
		
		
		if(views==null){
			throw new ConfigurationException("views为空");
		}
		
		List<Element> list = views.elements();
		
		if(list==null){
			throw new ConfigurationException("view为空");
		}
		
		HashMap<String,View> viewMapper = new HashMap<>();
		
		for (Element view : list) {
			
			View viewtype = new View();
			
			String viewName = view.attributeValue("name");
			String method = view.attributeValue("method");
			
			viewtype.setViewName(viewName);
			viewtype.setMethod(method);
			
			//****************************  relustname  ************************
			List<Element> relustNameList = view.elements("relustname");
			if(relustNameList!=null){
				for (Element mapper : relustNameList) {
					String property = mapper.attributeValue("property");
					String column = mapper.attributeValue("column");
					HashMap<String,String> map = viewtype.getRelustName();
					map.put(column, property);
				}
			}
			
			//****************************  address  ************************
			Element address = view.element("address");
			String addressValue = address.getText();

			viewtype.setAddress(addressValue);
			//****************************  mapper  ************************
			List<Element> mappers = view.elements("mapper");
			if(mappers!=null){
				for (Element mapper : mappers) {
					String property = mapper.attributeValue("property");
					String column = mapper.attributeValue("column");
					HashMap<String,String> map = viewtype.getMapper();
					map.put(property, column);
				}
			}
			//****************************  where  ************************
			
			List<Element> wheres = view.elements("where");
			if(wheres!=null){
				for (Element where : wheres) {
					String property = where.attributeValue("property");
					String column = where.attributeValue("column");
					String methodsql = where.attributeValue("method");
					if(methodsql==null){
						methodsql="";
					}
					HashMap<String,String> map = viewtype.getMapperwhere();
					map.put(property, column+":"+methodsql);
				}
			}
			//****************************  condition  ************************
			
			Element condition = view.element("condition");
			if(condition!=null){
				String conditionValue = condition.getText();
				viewtype.setCondition(conditionValue);
			}
			
			
			viewMapper.put(viewName, viewtype);
		}
		return viewMapper;
		
	}
	
	
	

}
